package com.xushuda.yas.storage;

import java.io.IOException;
import java.util.Comparator;
import java.util.NavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;

public class SliceFactory {

    public static MemSlice getMemSliceInstance(final Comparator<String> cmp, boolean isImmutable) {
        // 如果传入true，那么会生成一个包含空数据的MemSlice
        // 注意下面这个闭包的实现在生成Map的时候都传入了Comparator
        return new MemSlice(isImmutable) {
            // non static init block
            {
                map = new ConcurrentSkipListMap<String, SeqData>(cmp);
            }

            // 这个策略是直接flush所有数据，生成新的空map在原来的类
            @Override
            protected NavigableMap<String, SeqData> getFlushMap() {
                NavigableMap<String, SeqData> tmpMap = map;
                map = new ConcurrentSkipListMap<String, SeqData>(cmp);
                return tmpMap;
            }
        };
    }

    public static PermSlice getPermSliceInstance(Comparator<String> cmp, String basedir) throws IOException {
        return new SSTStorageEngine(cmp, basedir);
    }
}
